import os
from typing import final


def read_dataset(filename):
    data_arr = []
    resource_arr = []
    resource_num = 0
    task_num = 0
    task_arr = []
    skill_types = 0
    print(filename)
    with open(filename, 'r') as f:
        for line in f:
            line = line.replace('\t', ' ')
            line_data = line.strip().split(' ')
            line_data = list(filter(None, line_data))
            data_arr.append(line_data)
    for index in range(len(data_arr)):
        if data_arr[index][0] == 'Resources:':
            resource_num = int(data_arr[index][1])
            resource_arr = [{}] * resource_num
        elif data_arr[index][0] == 'Tasks:':
            task_num = int(data_arr[index][1])
            task_arr = [{}] * task_num
        elif data_arr[index][0] == 'Number':
            skill_types = int(data_arr[index][4])
        elif data_arr[index][0] == 'ResourceID':  # 读取资源
            for i in range(resource_num):
                data_resource_arr = data_arr[index + i + 1]
                skill_arr = [-1] * skill_types
                # print('skill_num ' + str(skill_types))
                for j in range(2, len(data_resource_arr), 2):
                    data_resource_arr[j] = data_resource_arr[j].replace('Q', '')
                    data_resource_arr[j] = data_resource_arr[j].replace(':', '')
                    # print(int(data_resource_arr[j]))
                    # print(skill_types)
                    skill_arr[int(data_resource_arr[j])] = int(data_resource_arr[j + 1])
                resource_arr[i] = {'id': int(data_resource_arr[0])-1, 'salary': float(data_resource_arr[1]), 'skill': skill_arr}
        elif data_arr[index][0] == "TaskID":
            for i in range(task_num):
                data_task_arr = data_arr[index + i + 1]
                predecessor_IDs = []
                for j in range(4, len(data_task_arr)):
                    predecessor_IDs.append(int(data_task_arr[j])-1)
                data_task_arr[2] = data_task_arr[2].replace(':', '')
                data_task_arr[2] = data_task_arr[2].replace('Q', '')
                task_arr[i] = {'id': int(data_task_arr[0])-1, 'duration': int(data_task_arr[1]),
                               'skill_type': int(data_task_arr[2]), 'skill_level': int(data_task_arr[3]),
                               'predecessors': predecessor_IDs}
    return resource_arr, task_arr, skill_types


def read_data():
    folder_path = r'G:\01. 发表学术论文\11. 多技能约束时变最短代价项目调度问题\code\MS_TCPSP\dataset'
    files_and_folder = os.listdir(folder_path)
    filename = ''
    for sub_folders in files_and_folder:
        dull_path_sub_folders = os.path.join(folder_path, sub_folders)
        sub_files_dir = os.listdir(dull_path_sub_folders)
        for file_dir in sub_files_dir:
            filename = os.path.join(dull_path_sub_folders, file_dir)
            resource_arr, task_arr, skill_num = read_dataset(filename)
            # print('filename: ' + str(resource_arr))
            # print('filename' + str(task_arr))
    return resource_arr, task_arr, skill_num
